package com.qfedu.hotelssm.controller;

import com.alibaba.excel.EasyExcel;
import com.qfedu.hotelssm.dto.HqRepairExportDto;
import com.qfedu.hotelssm.entity.HqRepair;
import com.qfedu.hotelssm.service.intf.HqRepairService;
import com.qfedu.hotelssm.vo.PageBean;
import com.qfedu.hotelssm.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author zlp
 * @date 2022/8/28 13:51
 */
@RestController
@RequestMapping("/api/repair/")
public class HqRepairController {
    @Autowired
    private HqRepairService service;


    //新增
    @PostMapping("save.do")
    public R save(HqRepair repair){
        return service.save(repair);
    }
    //更新
    @PostMapping("update.do")
    public R update(HqRepair repair){
        return service.update(repair);
    }
    //删除
    @GetMapping("del.do")
    public R del(int id){
        return service.delete(id);
    }
    //查询
    @GetMapping("page.do")
    public PageBean page(String departname, int page, int limit){
        return service.pageByName(departname,page,limit);
    }
    //导出数据
    @GetMapping("download.do")
    public void download(HttpServletResponse response) throws IOException {
        //1.设置响应头数据
        response.setHeader("content-disposition","attachment;filename="+System.currentTimeMillis()+".xlsx");
        //2.获取数据库中的数据
        List<HqRepairExportDto> list=service.all();
        //3.导出数据
        EasyExcel.write(response.getOutputStream(),HqRepairExportDto.class).sheet("Repair").doWrite(list);

    }
    //导入数据
    @PostMapping("upload.do")
    public R upload(MultipartFile file) throws IOException {
        return  service.upload(file);
    }
    //批量删除
    @PostMapping("delbyids.do")
    public R delByIds(int[] ids){
        return service.delByIds(ids);
    }

}
